/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU AFFERO General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
or see http://www.gnu.org/licenses/agpl.txt.
*/

// OSRM MainGUI
// [handles all GUI events that interact with appearance of main window]


OSRM.GUI.extend( {
		
// init GUI
init: function() {
	//init units selector
	OSRM.GUI.initDistanceFormatsSelector();
},

// reposition and hide zoom controls before main box animation
beforeMainTransition: function() {
	OSRM.G.map.zoomControl.hide();
},
// show zoom controls after main box animation
afterMainTransition: function() {
	OSRM.G.map.zoomControl.show();
},

// distance format routines
initDistanceFormatsSelector: function() {
	var options = OSRM.GUI.getDistanceFormats();
	//OSRM.GUI.selectorInit( "gui-units-toggle", options, OSRM.DEFAULTS.DISTANCE_FORMAT, OSRM.GUI._onDistanceFormatChanged );	
},
setDistanceFormat: function(type) {
	if( OSRM.G.active_distance_format == type )
		return;
	OSRM.G.active_distance_format = type;
	
	// change scale control
	if(OSRM.G.map) {
		OSRM.G.map.scaleControl.removeFrom(OSRM.G.map);
		OSRM.G.map.scaleControl.options.metric = (type != 1);
		OSRM.G.map.scaleControl.options.imperial = (type == 1);
		OSRM.G.map.scaleControl.addTo(OSRM.G.map);
	}
	
	// change converter
	if( type == 1 )
		OSRM.Utils.toHumanDistance = OSRM.Utils.toHumanDistanceMiles;
	else
		OSRM.Utils.toHumanDistance = OSRM.Utils.toHumanDistanceMeters;
},
_onDistanceFormatChanged: function(type) {
	OSRM.GUI.setDistanceFormat(type);
	OSRM.Routing.getRoute({keepAlternative:true});
},
setDistanceFormatsLanguage: function() {
	var options = OSRM.GUI.getDistanceFormats();
	OSRM.GUI.selectorRenameOptions("gui-units-toggle", options );	
},
getDistanceFormats: function() {
	return [{display:OSRM.loc("GUI_KILOMETERS"),value:0},{display:OSRM.loc("GUI_MILES"),value:1}];
},

// data timestamp routines
queryDataTimestamp: function() {
	OSRM.G.data_timestamp = "n/a";	
	OSRM.JSONP.call( OSRM.G.active_routing_timestamp_url+"?jsonp=%jsonp", OSRM.GUI.setDataTimestamp, OSRM.JSONP.empty, OSRM.DEFAULTS.JSONP_TIMEOUT, 'data_timestamp');	
},
setDataTimestamp: function(response) {
	if(!response)
		return;

	OSRM.G.data_timestamp = response.timestamp.slice(0,25).replace(/<\/?[^>]+(>|$)/g ,"");	// discard tags
    //document.getElementById('gui-data-timestamp').innerHTML = OSRM.G.data_timestamp;
}

});